#!/usr/bin/perl

use strict;

my @drills;
my $drill_id;
my $drill_r;

# read the drill tools
open(ACFILE, "<", "../circuit/gerbers/onehand_wireless.drl");
while (<ACFILE>)
{
	chomp;

	last if /\%/;

	if (/([T\d]+)C([\d\.]+)/)
	{
		$drill_id = $1;
		$drill_r = $2;
		push(@drills, $drill_id, $drill_r * 25.4 / 2);
	}
}

# read the drills positions
while (<ACFILE>)
{
	chomp;

	last if /T1/;
}

# read the drills positions
my $drill_ndx = 0;
$drill_r = @drills[$drill_ndx + 1];

# these are the PCB edges
my $offset_x = -0.95;
my $offset_y = 0.9275;

while (<ACFILE>)
{
	chomp;

	if (/X(\-?[\d]+)Y(\-?[\d]+)/)
	{
		print "translate([", ($1 / 10000 + $offset_x) * 25.4, ", ", ($2 / 10000 + $offset_y) * 25.4 + 99, ", -0.5])",
				" cylinder(r = ", $drill_r, ", h = pcb_thick+1);\n";
	} elsif ($_ = @drills[$drill_ndx]) {
		# change the current drill
		$drill_ndx = $drill_ndx + 2;
		$drill_r = @drills[$drill_ndx + 1];
	}
}

close(ACFILE);
